package com.liu.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liu.model.Ac;
import com.liu.model.Conn;

public class DbConn {
	static String[] zd = { "title", "lx", "dz", "txmy","state" };
	static int[] type = { 1, 1, 1, 1 ,2};

	public static String getDbCreateS() {
		String res = "create table conn(";
		res += "UUID char(32) primary key,";

		for (int i = 0; i < zd.length; i++) {
			res += zd[i];
			if (type[i] == 1) {
				res += " varchar(2000)";
			} else {
				res += " bigint";
			}
			if (i != zd.length - 1) {
				res += ",";
			}
		}
		res += ")";
		return res;
	}

	public static List<Object[]> del(Conn ac) {
		List<Object[]> res = new ArrayList<Object[]>();

		String res1 = "delete from conn where UUID=?";
		Object[] obj1 = new Object[2];
		obj1[0] = ac.getUuid();
		obj1[1] = res1;
		res.add(obj1);
		return res;
	}

	public static List<Object[]> AddRawData(Conn ac) {

		List<Object[]> res = new ArrayList<Object[]>();

		String res1 = "delete from conn where UUID=?";
		Object[] obj1 = new Object[2];
		obj1[0] = ac.getUuid();
		obj1[1] = res1;
		res.add(obj1);

		String res2 = "insert into conn(UUID,title,lx,dz,txmy,state) values(?,?,?,?,?,?)";
		Object[] obj2 = new Object[7];
		obj2[0] = ac.getUuid();
		obj2[1] = ac.getTitle();
		obj2[2] = ac.getLx();
		obj2[3] = ac.getDz();
		obj2[4] = ac.getTxmy();
		obj2[5] = ac.getState();
		obj2[6] = res2;
		res.add(obj2);

		return res;
	}

	public static Conn get(SQLiteDatabase dbms, String uuid) {
		String selStr = "select UUID,title,lx,dz,txmy,state from conn where UUID=?";
		Cursor cursor = dbms.rawQuery(selStr, new String[] { uuid });
		if (cursor.moveToNext()) {
			Conn ac = new Conn();
			ac.setUuid(cursor.getString(0));
			ac.setTitle(cursor.getString(1));
			ac.setLx(cursor.getString(2));
			ac.setDz(cursor.getString(3));
			ac.setTxmy(cursor.getString(4));
			ac.setState(cursor.getInt(5));
			return ac;
		}
		return null;
	}

	public static List<Conn> Select(SQLiteDatabase dbms) {
		List<Conn> res = new ArrayList<Conn>();
		String selStr = "select UUID,title,lx,dz,txmy,state from conn";
		Cursor cursor = dbms.rawQuery(selStr, new String[] {});
		while (cursor.moveToNext()) {
			Conn ac = new Conn();
			ac.setUuid(cursor.getString(0));
			ac.setTitle(cursor.getString(1));
			ac.setLx(cursor.getString(2));
			ac.setDz(cursor.getString(3));
			ac.setTxmy(cursor.getString(4));
			ac.setState(cursor.getInt(5));
			res.add(ac);
		}
		return res;
	}

}
